#ifndef USBTESTNODE_H
#define USBTESTNODE_H
#include "cocos2d.h"

USING_NS_CC;

class USBTestNode : public CCSprite
{
public:
	CREATE_FUNC(USBTestNode)
public:
	bool init()
	{
		bool bRet = false;
		do {
			// base
			CC_BREAK_IF(!CCSprite::initWithFile("usb/usb.png"));
			const CCSize winSize = CCDirector::sharedDirector()->getWinSize();
			this->setPosition(ccp(winSize.width /2 , winSize.height / 2));

			// mask
			CCSprite* mask = CCSprite::create("usb/mask.png");
			mask->setAnchorPoint(CCPointZero);
			m_maskSize = mask->getContentSize();
			CCClippingNode* cpn = CCClippingNode::create(mask);
			cpn->setAnchorPoint(CCPointZero);
			cpn->setPosition(CCPointZero);
			cpn->setAlphaThreshold(0.35f);
			this->addChild(cpn);

			m_flow = CCSprite::create("usb/wave.png");
			m_flow->setAnchorPoint(ccp(0, 1));
			cpn->addChild(m_flow);

			m_flow2 = CCSprite::create("usb/wave.png");
			m_flow2->setAnchorPoint(ccp(0, 1));
			cpn->addChild(m_flow2);
			m_flow2->setPositionX(m_flow->getContentSize().width);

			CCLabelTTF* lb = CCLabelTTF::create("", "DroidSansFallbackFull", 25);
			lb->setAnchorPoint(CCPointZero);
			lb->setPosition(ccp(0, -32));
			addChild(lb, 0, 1);

			setPercent(0);
			this->schedule(schedule_selector(USBTestNode::flowing));

			// light
			CCSprite* l = CCSprite::create("usb/light.png");
			l->setAnchorPoint(CCPointZero);
			l->setPosition(CCPointZero);
			this->addChild(l, 1);
			bRet = true;
		}while(0);
		return bRet;
	}
	// 0 - 100
	void		setPercent(float percent)
	{
		m_flow->setPositionY((percent / 100.0f) * m_flow->getContentSize().height);
		m_flow2->setPositionY(m_flow->getPositionY());
		CCLabelTTF* lbl = (CCLabelTTF*)this->getChildByTag(1);
		char str[8] = {0};
		sprintf(str, "%d%%", (int)percent);
		lbl->setString(str);
		int p = (int)percent;
		if (p == 85 || p == 0) {
			ccBlendFunc a = {GL_ONE, (p > 0) ? GL_ONE : CC_BLEND_DST};
			m_flow->setBlendFunc(a);
			m_flow2->setBlendFunc(a);
		}
	}
private:
	void		flowing(float dt)
	{
		if (m_flow2->getPositionX() <= 0) {
			m_flow->setPositionX(m_flow2->getContentSize().width);
			CC_SWAP(m_flow, m_flow2, CCSprite*);
		}
		else {
			m_flow->setPositionX(m_flow->getPositionX() - 1);
			m_flow2->setPositionX(m_flow2->getPositionX() - 1);
		}
		m_percent += 0.2f;
		if (m_percent > 100.0f) {
			m_percent = 0;
		}
		setPercent(m_percent);
	}

private:
	float		m_percent;
	CCSize		m_maskSize;
	CCSprite*	m_flow;
	CCSprite*	m_flow2;
};


#endif // USBTESTNODE_H

